/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-12-20 10:11
**/
package client

import (
	"ydd-im/service/api/define"
	"ydd-im/service/group/proto"
	"ydd-im/util/logger"
	"context"
	"fmt"
	"github.com/afex/hystrix-go/hystrix"
	"go.uber.org/zap"
	"strings"
)

/**
 * 创建群
 * @access  public
 * @param   int     userid  用户自增id
 * @param   int     sid  群签名id
 * @param   string     groupkey  群id
 * @param   []string     members  用户，多个以逗号隔开
 * @param   string     Name  群名称
 * @return  mix
 */
func AddGroupIsCluster(userid, sid int, groupkey, name string, members []string) (respon_data *proto.CreategroupResp, err error) {
	addr, err := GetBalanceAddrPlus(define.ETCD_SERVER_Group_LIST, true)
	if err != nil {
		return respon_data, err
	}
	fmt.Println("创建群集群地址",addr)
	conn, err := grpcConnPlus(addr)
	defer conn.Close()
	if err != nil {
		return respon_data, err
	}
	hystrix.ConfigureCommand("AddGroup", hystrix.CommandConfig{
		Timeout:                1000,
		MaxConcurrentRequests:  100,
		RequestVolumeThreshold: 50,
		ErrorPercentThreshold:  25,
	})
	fmt.Println(strings.Join(members,","))
	dataChan := make(chan *proto.CreategroupResp, 1)
	err = hystrix.Do("AddGroup", func() error {
		c := proto.NewGroupServiceClient(conn)
		resp, err := c.Creategroup(context.Background(), &proto.CreategroupReq{
			Id:      groupkey,
			Name:    name,
			Members: strings.Join(members, ","),
			Userid:  int64(userid),
			Sid:     int64(sid),
		})
		if err != nil {
			return err
		}
		dataChan <- resp
		return nil
	}, nil) // fallback没有处理错误请置为空，否则会处理错误
	if err != nil {
		logger.GetLg().Error("failed to call",
			zap.Any("err", err),
		)
		return respon_data, err
	}
	if list, ok := <-dataChan; ok {
		close(dataChan)
		return list, nil
	}
	close(dataChan)
	return respon_data, nil
}

/**
 * 加入群
 * @access  public
 * @param   int     userid  用户自增id
 * @param   int     sid  群签名id
 * @param   string     groupkey  群id
 * @param   string     Name  群名称
 * @param   []string     members  用户，多个以逗号隔开
 * @return  mix
 */
func AddGroupContactIsCluster(userid, sid int, groupkey string, members []string) (respon_data *proto.JoinGroupResp, err error) {
	addr, err := GetBalanceAddrPlus(define.ETCD_SERVER_Group_LIST, true)
	if err != nil {
		return respon_data, err
	}
	conn, err := grpcConnPlus(addr)
	defer conn.Close()
	if err != nil {
		return respon_data, err
	}
	hystrix.ConfigureCommand("JoinGroup", hystrix.CommandConfig{
		Timeout:                1000,
		MaxConcurrentRequests:  100,
		RequestVolumeThreshold: 50,
		ErrorPercentThreshold:  25,
	})
	dataChan := make(chan *proto.JoinGroupResp, 1)
	err = hystrix.Do("JoinGroup", func() error {
		c := proto.NewGroupServiceClient(conn)
		resp, err := c.JoinGroup(context.Background(), &proto.JoinGroupReq{
			Userid:  int64(userid),
			Sid:     int64(sid),
			Id:      groupkey,
			Members: strings.Join(members, ","),
		})
		if err != nil {
			return err
		}
		dataChan <- resp
		return nil
	}, nil) // fallback没有处理错误请置为空，否则会处理错误
	if err != nil {
		logger.GetLg().Error("failed to call",
			zap.Any("err", err),
		)
		return respon_data, err
	}
	if list, ok := <-dataChan; ok {
		close(dataChan)
		return list, nil
	}
	close(dataChan)
	return respon_data, nil
}
